Contents
  1. 1. Introduction
  2. 2. 相关知识
  3. 3. 解决方法

Introduction

用代码获取tomcat server的ip和端口号,注意这不是servlet中用request请求的客户端的ip地址。是代码运行所在的tomcat的真实ip地址。

相关知识

本代码只是写出了获取端口号和局域网ip地址的方法,若想取出真实ip地址,可以利用InetAddress类提供的接口进行判断!顺便贴下找到的关于IP地址段的知识。

1
2
3
4
5
6
7
8
9
10
11
Unicast
? AnyLocalAddress: 0.0.0.0
? LinkLocalAddress: 169.254.0.0 – 169.254.255.255
? LoopbackAddress: 127.0.0.0 – 127.255.255.255
? SiteLocalAddress: 10.0.0.0 – 10.255.255.255, 172.16.0.0 – 172.31.255.255, 192.168.0.0 – 192.168.255.255
– Multicast: 224.0.0.0 – 239.255.255.255
? MulticastGlobal: 224.0.1.0 – 238.255.255.255
? MulticastLinkLocal: 224.0.0.0 – 224.0.0.255 and ttl = 1
? MulticastNodeLocal: never, unless ttl = 0
? MulticastOrgLocal: 239.192.0.0 – 229.195.255.255
? MulticastSiteLocal: 239.255.0.0 – 239.255.255.255 or ttl < 32

解决方法

下面记录写出的工具类

java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.business.common.util;

import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Set;

import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.Query;

import org.apache.commons.lang.StringUtils;

public class IPAddressAndPortUtils {
private static final Log LOG = Log.getLog(IPAddressAndPortUtils.class);

public static String getTomcatPort() {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> objs = null;
try {
objs = mbs.queryNames(new ObjectName("*:type=Connector,*"),
Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
} catch (MalformedObjectNameException e) {
LOG.error("A MalformedObjectNameException is invoked.", e);
} catch (NullPointerException e) {
LOG.error("A NullPointerException is invoked.", e);
}
LOG.debug("Objs size : {}", objs.size());

String port = null;
for (Iterator<ObjectName> i = objs.iterator(); i.hasNext(); ) {
ObjectName obj = i.next();
port = obj.getKeyProperty("port");

if (StringUtils.isNotBlank(port)) {
return port;
}
}

return port;
}

public static String getServerSiteLocalAddress() {
Enumeration<NetworkInterface> networkInterfaces = null;
try {
networkInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
LOG.error("A SocketException is invoked.", e);
}
String tomcatIp = null;
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = (NetworkInterface) networkInterfaces.nextElement();
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();

while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = (InetAddress) inetAddresses.nextElement();

if (inetAddress.isSiteLocalAddress()) {
tomcatIp = inetAddress.getHostAddress();
LOG.debug("Web server ip : {}.", tomcatIp);
break;
}
}
}

return tomcatIp;
}

}
Contents
  1. 1. Introduction
  2. 2. 相关知识
  3. 3. 解决方法